using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using RegExEditor.Properties;

namespace RegExEditor
{
    /// <summary>
    /// The main form for the application.
    /// </summary>
    public class frmMain : System.Windows.Forms.Form
    {
        #region [Private variables]
        private bool hasMatches;
        private DataTable dtMatchesRes;
        private StringBuilder sbMatchesRes;
        private string preReplacementText;
        #endregion

        #region [VS Generated Code]
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtRegEx;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtReplace;
        private System.Windows.Forms.CheckBox cbIgnoreCase;
        private System.Windows.Forms.Button btnReplace;
        private System.Windows.Forms.Button btnMatches;
        private System.Windows.Forms.TextBox txtResult;
        private System.Windows.Forms.CheckBox cbCompiled;
        private System.Windows.Forms.CheckBox cbCultureInvariant;
        private System.Windows.Forms.CheckBox cbIgnoreWhiteSpace;
        private System.Windows.Forms.CheckBox cbECMAScript;
        private System.Windows.Forms.CheckBox cbExplicitCapture;
        private System.Windows.Forms.CheckBox cbMultiline;
        private System.Windows.Forms.CheckBox cbRightToLeft;
        private System.Windows.Forms.CheckBox cbSingleLine;
        private System.Windows.Forms.CheckBox cbMatchResultsAsGrid;
        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem FileMenu;
        private System.Windows.Forms.MenuItem FileExitMenu;
        private IContainer components;
        private System.Windows.Forms.MenuItem HelpMenu;
        private System.Windows.Forms.MenuItem HelpAboutMenu;
        private System.Windows.Forms.MenuItem EditMenu;
        private System.Windows.Forms.MenuItem EditCopyRegexMenu;
        private System.Windows.Forms.MenuItem EditCopyRegExCSMenu;
        private System.Windows.Forms.ContextMenu regExPopupMenu;
        private System.Windows.Forms.MenuItem menuItem4;
        private System.Windows.Forms.MenuItem menuItem9;
        private System.Windows.Forms.MenuItem RegExPopCopyCSMenu;
        private System.Windows.Forms.MenuItem RegExPopCopyVBMenu;
        private System.Windows.Forms.MenuItem RegExPopUndoMenu;
        private System.Windows.Forms.MenuItem RegExPopCutMenu;
        private System.Windows.Forms.MenuItem RegExPopCopyMenu;
        private System.Windows.Forms.MenuItem RegExPopPasteMenu;
        private System.Windows.Forms.MenuItem RegExPopSelectAllMenu;
        private System.Windows.Forms.MenuItem RegExPopDeleteMenu;
        private System.Windows.Forms.MenuItem EditCopyRegExVBMenu;
        private System.Windows.Forms.MenuItem FileOpenTestMenu;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.MenuItem FileSaveResultsAsMenu;
        private System.Windows.Forms.OpenFileDialog openFileDialog;
        private System.Windows.Forms.SaveFileDialog saveFileDialog;
        private SplitContainer splitContainer1;
        private SplitContainer splitContainer2;
        private SplitContainer splitContainer3;
        private FlowLayoutPanel flowLayoutPanel1;
        private ToolTip optionToolTip;
        private CheckBox cbMatchImmediately;
        private RichTextBox rtbTestText;
        private System.Windows.Forms.MenuItem HelpFeedbackMenu;
        private DataGridView grdResult;
        private Label lblIsMatchResult;
        private Label lblIsMatch;
        private CheckBox cbGroupsVisibility;
        private SplitContainer splitContainer4;
        private RichTextBox quickGuideRtb;

        private Random random;

        public frmMain()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            random = new Random();
            this.hasMatches = false;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components == null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
            this.splitContainer3 = new System.Windows.Forms.SplitContainer();
            this.cbMatchImmediately = new System.Windows.Forms.CheckBox();
            this.txtRegEx = new System.Windows.Forms.TextBox();
            this.regExPopupMenu = new System.Windows.Forms.ContextMenu();
            this.RegExPopUndoMenu = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.RegExPopCutMenu = new System.Windows.Forms.MenuItem();
            this.RegExPopCopyMenu = new System.Windows.Forms.MenuItem();
            this.RegExPopCopyCSMenu = new System.Windows.Forms.MenuItem();
            this.RegExPopCopyVBMenu = new System.Windows.Forms.MenuItem();
            this.RegExPopPasteMenu = new System.Windows.Forms.MenuItem();
            this.RegExPopDeleteMenu = new System.Windows.Forms.MenuItem();
            this.menuItem9 = new System.Windows.Forms.MenuItem();
            this.RegExPopSelectAllMenu = new System.Windows.Forms.MenuItem();
            this.label1 = new System.Windows.Forms.Label();
            this.txtReplace = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.splitContainer2 = new System.Windows.Forms.SplitContainer();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.rtbTestText = new System.Windows.Forms.RichTextBox();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.cbGroupsVisibility = new System.Windows.Forms.CheckBox();
            this.grdResult = new System.Windows.Forms.DataGridView();
            this.cbMatchResultsAsGrid = new System.Windows.Forms.CheckBox();
            this.txtResult = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.lblIsMatchResult = new System.Windows.Forms.Label();
            this.lblIsMatch = new System.Windows.Forms.Label();
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.cbIgnoreCase = new System.Windows.Forms.CheckBox();
            this.cbIgnoreWhiteSpace = new System.Windows.Forms.CheckBox();
            this.cbECMAScript = new System.Windows.Forms.CheckBox();
            this.cbRightToLeft = new System.Windows.Forms.CheckBox();
            this.cbCultureInvariant = new System.Windows.Forms.CheckBox();
            this.cbCompiled = new System.Windows.Forms.CheckBox();
            this.cbExplicitCapture = new System.Windows.Forms.CheckBox();
            this.cbMultiline = new System.Windows.Forms.CheckBox();
            this.cbSingleLine = new System.Windows.Forms.CheckBox();
            this.btnReplace = new System.Windows.Forms.Button();
            this.btnMatches = new System.Windows.Forms.Button();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.FileMenu = new System.Windows.Forms.MenuItem();
            this.FileOpenTestMenu = new System.Windows.Forms.MenuItem();
            this.FileSaveResultsAsMenu = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.FileExitMenu = new System.Windows.Forms.MenuItem();
            this.EditMenu = new System.Windows.Forms.MenuItem();
            this.EditCopyRegexMenu = new System.Windows.Forms.MenuItem();
            this.EditCopyRegExCSMenu = new System.Windows.Forms.MenuItem();
            this.EditCopyRegExVBMenu = new System.Windows.Forms.MenuItem();
            this.HelpMenu = new System.Windows.Forms.MenuItem();
            this.HelpFeedbackMenu = new System.Windows.Forms.MenuItem();
            this.HelpAboutMenu = new System.Windows.Forms.MenuItem();
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
            this.optionToolTip = new System.Windows.Forms.ToolTip(this.components);
            this.splitContainer4 = new System.Windows.Forms.SplitContainer();
            this.quickGuideRtb = new System.Windows.Forms.RichTextBox();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer3)).BeginInit();
            this.splitContainer3.Panel1.SuspendLayout();
            this.splitContainer3.Panel2.SuspendLayout();
            this.splitContainer3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
            this.splitContainer2.Panel1.SuspendLayout();
            this.splitContainer2.Panel2.SuspendLayout();
            this.splitContainer2.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdResult)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.flowLayoutPanel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer4)).BeginInit();
            this.splitContainer4.Panel1.SuspendLayout();
            this.splitContainer4.Panel2.SuspendLayout();
            this.splitContainer4.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer3
            // 
            this.splitContainer3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.splitContainer3.Location = new System.Drawing.Point(7, 16);
            this.splitContainer3.Name = "splitContainer3";
            this.splitContainer3.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer3.Panel1
            // 
            this.splitContainer3.Panel1.Controls.Add(this.cbMatchImmediately);
            this.splitContainer3.Panel1.Controls.Add(this.txtRegEx);
            this.splitContainer3.Panel1.Controls.Add(this.label1);
            // 
            // splitContainer3.Panel2
            // 
            this.splitContainer3.Panel2.Controls.Add(this.txtReplace);
            this.splitContainer3.Panel2.Controls.Add(this.label2);
            this.splitContainer3.Size = new System.Drawing.Size(286, 338);
            this.splitContainer3.SplitterDistance = 168;
            this.splitContainer3.TabIndex = 0;
            // 
            // cbMatchImmediately
            // 
            this.cbMatchImmediately.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.cbMatchImmediately.AutoSize = true;
            this.cbMatchImmediately.Checked = true;
            this.cbMatchImmediately.CheckState = System.Windows.Forms.CheckState.Checked;
            this.cbMatchImmediately.Location = new System.Drawing.Point(169, 2);
            this.cbMatchImmediately.Name = "cbMatchImmediately";
            this.cbMatchImmediately.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.cbMatchImmediately.Size = new System.Drawing.Size(113, 17);
            this.cbMatchImmediately.TabIndex = 1;
            this.cbMatchImmediately.Text = "Match immediately";
            this.optionToolTip.SetToolTip(this.cbMatchImmediately, "Found search will be shown immediately");
            this.cbMatchImmediately.UseVisualStyleBackColor = true;
            // 
            // txtRegEx
            // 
            this.txtRegEx.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtRegEx.BackColor = System.Drawing.SystemColors.Window;
            this.txtRegEx.ContextMenu = this.regExPopupMenu;
            this.txtRegEx.Location = new System.Drawing.Point(0, 20);
            this.txtRegEx.Multiline = true;
            this.txtRegEx.Name = "txtRegEx";
            this.txtRegEx.Size = new System.Drawing.Size(282, 149);
            this.txtRegEx.TabIndex = 2;
            this.txtRegEx.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtRegEx_KeyUp);
            // 
            // regExPopupMenu
            // 
            this.regExPopupMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.RegExPopUndoMenu,
            this.menuItem4,
            this.RegExPopCutMenu,
            this.RegExPopCopyMenu,
            this.RegExPopCopyCSMenu,
            this.RegExPopCopyVBMenu,
            this.RegExPopPasteMenu,
            this.RegExPopDeleteMenu,
            this.menuItem9,
            this.RegExPopSelectAllMenu});
            this.regExPopupMenu.Popup += new System.EventHandler(this.regExPopupMenu_Popup);
            // 
            // RegExPopUndoMenu
            // 
            this.RegExPopUndoMenu.Index = 0;
            this.RegExPopUndoMenu.Text = "&Undo";
            this.RegExPopUndoMenu.Click += new System.EventHandler(this.RegExPopUndoMenu_Click);
            // 
            // menuItem4
            // 
            this.menuItem4.Index = 1;
            this.menuItem4.Text = "-";
            // 
            // RegExPopCutMenu
            // 
            this.RegExPopCutMenu.Index = 2;
            this.RegExPopCutMenu.Text = "Cu&t";
            this.RegExPopCutMenu.Click += new System.EventHandler(this.RegExPopCutMenu_Click);
            // 
            // RegExPopCopyMenu
            // 
            this.RegExPopCopyMenu.Index = 3;
            this.RegExPopCopyMenu.Text = "&Copy";
            this.RegExPopCopyMenu.Click += new System.EventHandler(this.RegExPopCopyMenu_Click);
            // 
            // RegExPopCopyCSMenu
            // 
            this.RegExPopCopyCSMenu.Index = 4;
            this.RegExPopCopyCSMenu.Text = "Copy as C&# ";
            this.RegExPopCopyCSMenu.Click += new System.EventHandler(this.RegExPopCopyCSMenu_Click);
            // 
            // RegExPopCopyVBMenu
            // 
            this.RegExPopCopyVBMenu.Index = 5;
            this.RegExPopCopyVBMenu.Text = "Copy as &VB";
            this.RegExPopCopyVBMenu.Click += new System.EventHandler(this.RegExPopCopyVBMenu_Click);
            // 
            // RegExPopPasteMenu
            // 
            this.RegExPopPasteMenu.Index = 6;
            this.RegExPopPasteMenu.Text = "&Paste";
            this.RegExPopPasteMenu.Click += new System.EventHandler(this.RegExPopPasteMenu_Click);
            // 
            // RegExPopDeleteMenu
            // 
            this.RegExPopDeleteMenu.Index = 7;
            this.RegExPopDeleteMenu.Text = "&Delete";
            this.RegExPopDeleteMenu.Click += new System.EventHandler(this.RegexPopDeleteMenu_Click);
            // 
            // menuItem9
            // 
            this.menuItem9.Index = 8;
            this.menuItem9.Text = "-";
            // 
            // RegExPopSelectAllMenu
            // 
            this.RegExPopSelectAllMenu.Index = 9;
            this.RegExPopSelectAllMenu.Text = "Select &All";
            this.RegExPopSelectAllMenu.Click += new System.EventHandler(this.RegExPopSelectAllMenu_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(1, 4);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(45, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Reg Ex:";
            // 
            // txtReplace
            // 
            this.txtReplace.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtReplace.ContextMenu = this.regExPopupMenu;
            this.txtReplace.Location = new System.Drawing.Point(0, 19);
            this.txtReplace.Multiline = true;
            this.txtReplace.Name = "txtReplace";
            this.txtReplace.Size = new System.Drawing.Size(282, 144);
            this.txtReplace.TabIndex = 1;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(1, 3);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(50, 13);
            this.label2.TabIndex = 0;
            this.label2.Text = "Replace:";
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.splitContainer2);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.groupBox1);
            this.splitContainer1.Size = new System.Drawing.Size(600, 527);
            this.splitContainer1.SplitterDistance = 300;
            this.splitContainer1.TabIndex = 0;
            // 
            // splitContainer2
            // 
            this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer2.Location = new System.Drawing.Point(0, 0);
            this.splitContainer2.Name = "splitContainer2";
            this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer2.Panel1
            // 
            this.splitContainer2.Panel1.Controls.Add(this.groupBox2);
            // 
            // splitContainer2.Panel2
            // 
            this.splitContainer2.Panel2.Controls.Add(this.groupBox3);
            this.splitContainer2.Size = new System.Drawing.Size(300, 527);
            this.splitContainer2.SplitterDistance = 267;
            this.splitContainer2.TabIndex = 0;
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.rtbTestText);
            this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox2.Location = new System.Drawing.Point(0, 0);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(300, 267);
            this.groupBox2.TabIndex = 0;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Text To Query";
            // 
            // rtbTestText
            // 
            this.rtbTestText.ContextMenu = this.regExPopupMenu;
            this.rtbTestText.Dock = System.Windows.Forms.DockStyle.Fill;
            this.rtbTestText.Location = new System.Drawing.Point(3, 16);
            this.rtbTestText.Name = "rtbTestText";
            this.rtbTestText.Size = new System.Drawing.Size(294, 248);
            this.rtbTestText.TabIndex = 0;
            this.rtbTestText.Text = "";
            this.rtbTestText.KeyDown += new System.Windows.Forms.KeyEventHandler(this.rtbTestText_KeyDown);
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.cbGroupsVisibility);
            this.groupBox3.Controls.Add(this.grdResult);
            this.groupBox3.Controls.Add(this.cbMatchResultsAsGrid);
            this.groupBox3.Controls.Add(this.txtResult);
            this.groupBox3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox3.Location = new System.Drawing.Point(0, 0);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(300, 256);
            this.groupBox3.TabIndex = 0;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Results";
            // 
            // cbGroupsVisibility
            // 
            this.cbGroupsVisibility.AutoSize = true;
            this.cbGroupsVisibility.Location = new System.Drawing.Point(180, 15);
            this.cbGroupsVisibility.Name = "cbGroupsVisibility";
            this.cbGroupsVisibility.Size = new System.Drawing.Size(90, 17);
            this.cbGroupsVisibility.TabIndex = 1;
            this.cbGroupsVisibility.Text = "Show Groups";
            this.cbGroupsVisibility.UseVisualStyleBackColor = true;
            this.cbGroupsVisibility.CheckedChanged += new System.EventHandler(this.cbGroupsVisibility_CheckedChanged);
            // 
            // grdResult
            // 
            this.grdResult.AllowUserToAddRows = false;
            this.grdResult.AllowUserToDeleteRows = false;
            this.grdResult.AllowUserToResizeRows = false;
            this.grdResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.grdResult.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
            this.grdResult.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.grdResult.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdResult.EnableHeadersVisualStyles = false;
            this.grdResult.Location = new System.Drawing.Point(3, 38);
            this.grdResult.MultiSelect = false;
            this.grdResult.Name = "grdResult";
            this.grdResult.ReadOnly = true;
            this.grdResult.RowHeadersVisible = false;
            this.grdResult.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.grdResult.ShowEditingIcon = false;
            this.grdResult.Size = new System.Drawing.Size(291, 212);
            this.grdResult.TabIndex = 2;
            // 
            // cbMatchResultsAsGrid
            // 
            this.cbMatchResultsAsGrid.Checked = true;
            this.cbMatchResultsAsGrid.CheckState = System.Windows.Forms.CheckState.Checked;
            this.cbMatchResultsAsGrid.Location = new System.Drawing.Point(3, 15);
            this.cbMatchResultsAsGrid.Name = "cbMatchResultsAsGrid";
            this.cbMatchResultsAsGrid.Size = new System.Drawing.Size(168, 17);
            this.cbMatchResultsAsGrid.TabIndex = 0;
            this.cbMatchResultsAsGrid.Text = "Show matches in grid";
            this.cbMatchResultsAsGrid.CheckedChanged += new System.EventHandler(this.cbMatchResultsAsGrid_CheckedChanged);
            // 
            // txtResult
            // 
            this.txtResult.AcceptsReturn = true;
            this.txtResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtResult.Location = new System.Drawing.Point(3, 38);
            this.txtResult.Multiline = true;
            this.txtResult.Name = "txtResult";
            this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtResult.Size = new System.Drawing.Size(291, 212);
            this.txtResult.TabIndex = 3;
            this.txtResult.Visible = false;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.lblIsMatchResult);
            this.groupBox1.Controls.Add(this.lblIsMatch);
            this.groupBox1.Controls.Add(this.flowLayoutPanel1);
            this.groupBox1.Controls.Add(this.splitContainer3);
            this.groupBox1.Controls.Add(this.btnReplace);
            this.groupBox1.Controls.Add(this.btnMatches);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(296, 527);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Query";
            // 
            // lblIsMatchResult
            // 
            this.lblIsMatchResult.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lblIsMatchResult.AutoSize = true;
            this.lblIsMatchResult.Font = new System.Drawing.Font("Wingdings 2", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            this.lblIsMatchResult.ForeColor = System.Drawing.Color.White;
            this.lblIsMatchResult.Location = new System.Drawing.Point(71, 494);
            this.lblIsMatchResult.Name = "lblIsMatchResult";
            this.lblIsMatchResult.Size = new System.Drawing.Size(0, 26);
            this.lblIsMatchResult.TabIndex = 3;
            // 
            // lblIsMatch
            // 
            this.lblIsMatch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lblIsMatch.AutoSize = true;
            this.lblIsMatch.Location = new System.Drawing.Point(8, 501);
            this.lblIsMatch.Name = "lblIsMatch";
            this.lblIsMatch.Size = new System.Drawing.Size(54, 13);
            this.lblIsMatch.TabIndex = 2;
            this.lblIsMatch.Text = "Is Match?";
            // 
            // flowLayoutPanel1
            // 
            this.flowLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.flowLayoutPanel1.Controls.Add(this.cbIgnoreCase);
            this.flowLayoutPanel1.Controls.Add(this.cbIgnoreWhiteSpace);
            this.flowLayoutPanel1.Controls.Add(this.cbECMAScript);
            this.flowLayoutPanel1.Controls.Add(this.cbRightToLeft);
            this.flowLayoutPanel1.Controls.Add(this.cbCultureInvariant);
            this.flowLayoutPanel1.Controls.Add(this.cbCompiled);
            this.flowLayoutPanel1.Controls.Add(this.cbExplicitCapture);
            this.flowLayoutPanel1.Controls.Add(this.cbMultiline);
            this.flowLayoutPanel1.Controls.Add(this.cbSingleLine);
            this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
            this.flowLayoutPanel1.Location = new System.Drawing.Point(7, 360);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(286, 129);
            this.flowLayoutPanel1.TabIndex = 1;
            // 
            // cbIgnoreCase
            // 
            this.cbIgnoreCase.AccessibleDescription = "";
            this.cbIgnoreCase.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbIgnoreCase.AutoSize = true;
            this.cbIgnoreCase.Location = new System.Drawing.Point(3, 3);
            this.cbIgnoreCase.Name = "cbIgnoreCase";
            this.cbIgnoreCase.Size = new System.Drawing.Size(83, 17);
            this.cbIgnoreCase.TabIndex = 0;
            this.cbIgnoreCase.Text = "Ignore Case";
            this.optionToolTip.SetToolTip(this.cbIgnoreCase, "Stops the RegEx from distinguishing between upper and lower case letters.\r\n\r\nEqui" +
        "vilant of (?i)");
            this.cbIgnoreCase.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // cbIgnoreWhiteSpace
            // 
            this.cbIgnoreWhiteSpace.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbIgnoreWhiteSpace.AutoSize = true;
            this.cbIgnoreWhiteSpace.Location = new System.Drawing.Point(3, 26);
            this.cbIgnoreWhiteSpace.Name = "cbIgnoreWhiteSpace";
            this.cbIgnoreWhiteSpace.Size = new System.Drawing.Size(153, 17);
            this.cbIgnoreWhiteSpace.TabIndex = 1;
            this.cbIgnoreWhiteSpace.Text = "Ignore Pattern Whitespace";
            this.optionToolTip.SetToolTip(this.cbIgnoreWhiteSpace, "Stops the pattern matching unescaped whitespace and allows # comments.\r\n\r\nEquivil" +
        "ant of (?x)");
            this.cbIgnoreWhiteSpace.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // cbECMAScript
            // 
            this.cbECMAScript.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbECMAScript.AutoSize = true;
            this.cbECMAScript.Location = new System.Drawing.Point(3, 49);
            this.cbECMAScript.Name = "cbECMAScript";
            this.cbECMAScript.Size = new System.Drawing.Size(86, 17);
            this.cbECMAScript.TabIndex = 2;
            this.cbECMAScript.Text = "ECMA Script";
            this.optionToolTip.SetToolTip(this.cbECMAScript, "Processes the Regular Expression in ECMA Script compaitble mode.\r\n\r\nNote: This ca" +
        "n only be used in conjuction with Ignore Case and Multiline.");
            this.cbECMAScript.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // cbRightToLeft
            // 
            this.cbRightToLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbRightToLeft.AutoSize = true;
            this.cbRightToLeft.Location = new System.Drawing.Point(3, 72);
            this.cbRightToLeft.Name = "cbRightToLeft";
            this.cbRightToLeft.Size = new System.Drawing.Size(88, 17);
            this.cbRightToLeft.TabIndex = 3;
            this.cbRightToLeft.Text = "Right To Left";
            this.optionToolTip.SetToolTip(this.cbRightToLeft, "Makes the Regular Expression start at the end of the  Queried text and work forwa" +
        "rd.");
            this.cbRightToLeft.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // cbCultureInvariant
            // 
            this.cbCultureInvariant.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbCultureInvariant.AutoSize = true;
            this.cbCultureInvariant.Location = new System.Drawing.Point(3, 95);
            this.cbCultureInvariant.Name = "cbCultureInvariant";
            this.cbCultureInvariant.Size = new System.Drawing.Size(103, 17);
            this.cbCultureInvariant.TabIndex = 4;
            this.cbCultureInvariant.Text = "Culture Invariant";
            this.optionToolTip.SetToolTip(this.cbCultureInvariant, "Ignore cultural differences in language.");
            this.cbCultureInvariant.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // cbCompiled
            // 
            this.cbCompiled.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbCompiled.AutoSize = true;
            this.cbCompiled.Location = new System.Drawing.Point(162, 3);
            this.cbCompiled.Name = "cbCompiled";
            this.cbCompiled.Size = new System.Drawing.Size(69, 17);
            this.cbCompiled.TabIndex = 5;
            this.cbCompiled.Text = "Compiled";
            this.optionToolTip.SetToolTip(this.cbCompiled, "Compiles the Regular Expression which slows down initialisation, but speeds up ex" +
        "ecution.");
            this.cbCompiled.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // cbExplicitCapture
            // 
            this.cbExplicitCapture.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbExplicitCapture.AutoSize = true;
            this.cbExplicitCapture.Location = new System.Drawing.Point(162, 26);
            this.cbExplicitCapture.Name = "cbExplicitCapture";
            this.cbExplicitCapture.Size = new System.Drawing.Size(99, 17);
            this.cbExplicitCapture.TabIndex = 6;
            this.cbExplicitCapture.Text = "Explicit Capture";
            this.optionToolTip.SetToolTip(this.cbExplicitCapture, resources.GetString("cbExplicitCapture.ToolTip"));
            this.cbExplicitCapture.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // cbMultiline
            // 
            this.cbMultiline.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbMultiline.AutoSize = true;
            this.cbMultiline.Location = new System.Drawing.Point(162, 49);
            this.cbMultiline.Name = "cbMultiline";
            this.cbMultiline.Size = new System.Drawing.Size(64, 17);
            this.cbMultiline.TabIndex = 7;
            this.cbMultiline.Text = "Multiline";
            this.optionToolTip.SetToolTip(this.cbMultiline, "Makes ^ and $ apply at the begining and end of every line in the Queried Text and" +
        " not just the start and end of the whole Query Text.\r\n\r\nEquivalent to (?m)");
            this.cbMultiline.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // cbSingleLine
            // 
            this.cbSingleLine.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.cbSingleLine.AutoSize = true;
            this.cbSingleLine.Location = new System.Drawing.Point(162, 72);
            this.cbSingleLine.Name = "cbSingleLine";
            this.cbSingleLine.Size = new System.Drawing.Size(71, 17);
            this.cbSingleLine.TabIndex = 8;
            this.cbSingleLine.Text = "Singleline";
            this.optionToolTip.SetToolTip(this.cbSingleLine, "Stops the dot (.) character ignoring the end of line character (\\n)\r\n\r\nEquivalent" +
        " of (?s)");
            this.cbSingleLine.CheckedChanged += new System.EventHandler(this.RegexOptions_CheckedChanged);
            // 
            // btnReplace
            // 
            this.btnReplace.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnReplace.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(215)))), ((int)(((byte)(187)))), ((int)(((byte)(91)))));
            this.btnReplace.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnReplace.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnReplace.ForeColor = System.Drawing.Color.Black;
            this.btnReplace.Location = new System.Drawing.Point(217, 495);
            this.btnReplace.Name = "btnReplace";
            this.btnReplace.Size = new System.Drawing.Size(72, 24);
            this.btnReplace.TabIndex = 5;
            this.btnReplace.Text = "Replace";
            this.btnReplace.UseVisualStyleBackColor = false;
            this.btnReplace.Click += new System.EventHandler(this.btnReplace_Click);
            // 
            // btnMatches
            // 
            this.btnMatches.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnMatches.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(215)))), ((int)(((byte)(187)))), ((int)(((byte)(91)))));
            this.btnMatches.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnMatches.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnMatches.ForeColor = System.Drawing.Color.Black;
            this.btnMatches.Location = new System.Drawing.Point(143, 495);
            this.btnMatches.Name = "btnMatches";
            this.btnMatches.Size = new System.Drawing.Size(72, 24);
            this.btnMatches.TabIndex = 4;
            this.btnMatches.Text = "Matches";
            this.btnMatches.UseVisualStyleBackColor = false;
            this.btnMatches.Click += new System.EventHandler(this.btnMatches_Click);
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.FileMenu,
            this.EditMenu,
            this.HelpMenu});
            // 
            // FileMenu
            // 
            this.FileMenu.Index = 0;
            this.FileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.FileOpenTestMenu,
            this.FileSaveResultsAsMenu,
            this.menuItem1,
            this.FileExitMenu});
            this.FileMenu.Text = "&File";
            this.FileMenu.Popup += new System.EventHandler(this.FileMenu_Popup);
            // 
            // FileOpenTestMenu
            // 
            this.FileOpenTestMenu.Index = 0;
            this.FileOpenTestMenu.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
            this.FileOpenTestMenu.Text = "Open Test File...";
            this.FileOpenTestMenu.Click += new System.EventHandler(this.FileOpenTestMenu_Click);
            // 
            // FileSaveResultsAsMenu
            // 
            this.FileSaveResultsAsMenu.Index = 1;
            this.FileSaveResultsAsMenu.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftS;
            this.FileSaveResultsAsMenu.Text = "Save Results As...";
            this.FileSaveResultsAsMenu.Click += new System.EventHandler(this.FileSaveResultsAsMenu_Click);
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 2;
            this.menuItem1.Text = "-";
            // 
            // FileExitMenu
            // 
            this.FileExitMenu.Index = 3;
            this.FileExitMenu.Shortcut = System.Windows.Forms.Shortcut.AltF4;
            this.FileExitMenu.Text = "E&xit";
            this.FileExitMenu.Click += new System.EventHandler(this.FileExitMenu_Click);
            // 
            // EditMenu
            // 
            this.EditMenu.Index = 1;
            this.EditMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.EditCopyRegexMenu,
            this.EditCopyRegExCSMenu,
            this.EditCopyRegExVBMenu});
            this.EditMenu.Text = "&Edit";
            this.EditMenu.Popup += new System.EventHandler(this.EditMenu_Popup);
            // 
            // EditCopyRegexMenu
            // 
            this.EditCopyRegexMenu.Index = 0;
            this.EditCopyRegexMenu.Text = "Copy Whole RegEx";
            this.EditCopyRegexMenu.Click += new System.EventHandler(this.EditCopyRegexMenu_Click);
            // 
            // EditCopyRegExCSMenu
            // 
            this.EditCopyRegExCSMenu.Index = 1;
            this.EditCopyRegExCSMenu.Text = "Copy Whole RegEx as C#";
            this.EditCopyRegExCSMenu.Click += new System.EventHandler(this.EditCopyRegExCSMenu_Click);
            // 
            // EditCopyRegExVBMenu
            // 
            this.EditCopyRegExVBMenu.Index = 2;
            this.EditCopyRegExVBMenu.Text = "Copy Whole RegEx as VB";
            this.EditCopyRegExVBMenu.Click += new System.EventHandler(this.EditCopyRegExVBMenu_Click);
            // 
            // HelpMenu
            // 
            this.HelpMenu.Index = 2;
            this.HelpMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.HelpFeedbackMenu,
            this.HelpAboutMenu});
            this.HelpMenu.Text = "&Help";
            // 
            // HelpFeedbackMenu
            // 
            this.HelpFeedbackMenu.Index = 0;
            this.HelpFeedbackMenu.Text = "Send &Feedback";
            this.HelpFeedbackMenu.Click += new System.EventHandler(this.HelpFeedbackMenu_Click);
            // 
            // HelpAboutMenu
            // 
            this.HelpAboutMenu.Index = 1;
            this.HelpAboutMenu.Text = "&About RegEx Editor";
            this.HelpAboutMenu.Click += new System.EventHandler(this.HelpAboutMenu_Click);
            // 
            // openFileDialog
            // 
            this.openFileDialog.DefaultExt = "txt";
            this.openFileDialog.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";
            this.openFileDialog.InitialDirectory = "C:\\";
            this.openFileDialog.RestoreDirectory = true;
            // 
            // saveFileDialog
            // 
            this.saveFileDialog.DefaultExt = "txt";
            this.saveFileDialog.Filter = "txt files (*.txt)|*.txt|CSV files ((.csv)|*.csv|XML files (*.xml)|*.xml|All files" +
    " (*.*)|*.*";
            this.saveFileDialog.SupportMultiDottedExtensions = true;
            this.saveFileDialog.Title = "Save Results As";
            // 
            // splitContainer4
            // 
            this.splitContainer4.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer4.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
            this.splitContainer4.Location = new System.Drawing.Point(5, 5);
            this.splitContainer4.Name = "splitContainer4";
            // 
            // splitContainer4.Panel1
            // 
            this.splitContainer4.Panel1.Controls.Add(this.splitContainer1);
            // 
            // splitContainer4.Panel2
            // 
            this.splitContainer4.Panel2.Controls.Add(this.quickGuideRtb);
            this.splitContainer4.Size = new System.Drawing.Size(793, 527);
            this.splitContainer4.SplitterDistance = 600;
            this.splitContainer4.TabIndex = 1;
            // 
            // quickGuideRtb
            // 
            this.quickGuideRtb.Dock = System.Windows.Forms.DockStyle.Fill;
            this.quickGuideRtb.Location = new System.Drawing.Point(0, 0);
            this.quickGuideRtb.Name = "quickGuideRtb";
            this.quickGuideRtb.ReadOnly = true;
            this.quickGuideRtb.Size = new System.Drawing.Size(189, 527);
            this.quickGuideRtb.TabIndex = 0;
            this.quickGuideRtb.Text = "";
            // 
            // frmMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(245)))), ((int)(((byte)(238)))), ((int)(((byte)(214)))));
            this.ClientSize = new System.Drawing.Size(803, 537);
            this.Controls.Add(this.splitContainer4);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Menu = this.mainMenu1;
            this.MinimumSize = new System.Drawing.Size(480, 472);
            this.Name = "frmMain";
            this.Padding = new System.Windows.Forms.Padding(5);
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
            this.Text = "RegEx Editor";
            this.Load += new System.EventHandler(this.frmMain_Load);
            this.splitContainer3.Panel1.ResumeLayout(false);
            this.splitContainer3.Panel1.PerformLayout();
            this.splitContainer3.Panel2.ResumeLayout(false);
            this.splitContainer3.Panel2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer3)).EndInit();
            this.splitContainer3.ResumeLayout(false);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.splitContainer2.Panel1.ResumeLayout(false);
            this.splitContainer2.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
            this.splitContainer2.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdResult)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.flowLayoutPanel1.ResumeLayout(false);
            this.flowLayoutPanel1.PerformLayout();
            this.splitContainer4.Panel1.ResumeLayout(false);
            this.splitContainer4.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer4)).EndInit();
            this.splitContainer4.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new frmMain());
        }
        #endregion

        private void frmMain_Load(object sender, System.EventArgs e)
        {
            txtResult.Text = string.Empty;

            // This is set here and not using the control's property
            // as visual studio's designer keep placing this line
            // above the one that specifies the whole control's width.
            // The result is you always get an exception when the form 
            // loads. So we have had to put it here instead.
            splitContainer1.Panel2MinSize = 290;

            quickGuideRtb.Rtf = Resources.QuickReference;
        }

        private void btnReplace_Click(object sender, System.EventArgs e)
        {
            try
            {
                RegexOptions regExOptions = GetRegExOptions();
                Regex regExTest = new Regex(txtRegEx.Text, regExOptions);
                preReplacementText = rtbTestText.Text;
                rtbTestText.Text = regExTest.Replace(rtbTestText.Text, txtReplace.Text);
            }
            catch (System.Exception ex)
            {
                txtResult.Text = ex.Message;
                cbMatchResultsAsGrid.Checked = false;
            }
        }

        private void btnMatches_Click(object sender, System.EventArgs e)
        {
            ApplyRegEx();
        }

        private void ClearResultBox()
        {
            txtResult.Visible = true;
            grdResult.Visible = false;
            txtResult.Text = string.Empty;
        }

        private RegexOptions GetRegExOptions()
        {
            RegexOptions regExOptions = RegexOptions.None;

            if (cbCompiled.Checked && cbCompiled.Enabled)
            {
                regExOptions |= RegexOptions.Compiled;
            }

            if (cbCultureInvariant.Checked && cbCultureInvariant.Enabled)
            {
                regExOptions |= RegexOptions.CultureInvariant;
            }

            if (cbECMAScript.Checked && cbECMAScript.Enabled)
            {
                regExOptions |= RegexOptions.ECMAScript;
            }

            if (cbExplicitCapture.Checked && cbExplicitCapture.Enabled)
            {
                regExOptions |= RegexOptions.ExplicitCapture;
            }

            if (cbIgnoreCase.Checked && cbIgnoreCase.Enabled)
            {
                regExOptions |= RegexOptions.IgnoreCase;
            }

            if (cbIgnoreWhiteSpace.Checked && cbIgnoreWhiteSpace.Enabled)
            {
                regExOptions |= RegexOptions.IgnorePatternWhitespace;
            }

            if (cbMultiline.Checked && cbMultiline.Enabled)
            {
                regExOptions |= RegexOptions.Multiline;
            }

            if (cbRightToLeft.Checked && cbRightToLeft.Enabled)
            {
                regExOptions |= RegexOptions.RightToLeft;
            }

            if (cbSingleLine.Checked && cbSingleLine.Enabled)
            {
                regExOptions |= RegexOptions.Singleline;
            }

            return regExOptions;
        }

        private void FileExitMenu_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        private void HelpAboutMenu_Click(object sender, System.EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog(this);
        }

        private void RegExPopSelectAllMenu_Click(object sender, System.EventArgs e)
        {
            ((System.Windows.Forms.TextBox)((System.Windows.Forms.MenuItem)sender).GetContextMenu().SourceControl).SelectAll();
        }

        private void RegExPopUndoMenu_Click(object sender, System.EventArgs e)
        {
            dynamic txtBox = ((System.Windows.Forms.MenuItem)sender).GetContextMenu().SourceControl;
            if (txtBox.CanUndo)
            {
                txtBox.Undo();
                txtBox.ClearUndo();
            }
            else
            {
                txtBox.Text = preReplacementText;
                preReplacementText = String.Empty;
            }
        }

        private void RegExPopCutMenu_Click(object sender, System.EventArgs e)
        {
            ((System.Windows.Forms.TextBox)((System.Windows.Forms.MenuItem)sender).GetContextMenu().SourceControl).Cut();
        }

        private void RegExPopCopyMenu_Click(object sender, System.EventArgs e)
        {
            ((System.Windows.Forms.TextBox)((System.Windows.Forms.MenuItem)sender).GetContextMenu().SourceControl).Copy();
        }

        private void RegExPopPasteMenu_Click(object sender, System.EventArgs e)
        {
            ((System.Windows.Forms.TextBox)((System.Windows.Forms.MenuItem)sender).GetContextMenu().SourceControl).Paste();
        }

        private void RegexPopDeleteMenu_Click(object sender, System.EventArgs e)
        {
            ((System.Windows.Forms.TextBox)((System.Windows.Forms.MenuItem)sender).GetContextMenu().SourceControl).SelectedText = string.Empty;
        }

        private void regExPopupMenu_Popup(object sender, System.EventArgs e)
        {
            ContextMenu contextMenu = ((System.Windows.Forms.ContextMenu)sender).GetContextMenu();
            dynamic txtBox = contextMenu.SourceControl;

            RegExPopUndoMenu.Enabled = txtBox.CanUndo || !String.IsNullOrEmpty(preReplacementText);

            bool isSelected = txtBox.SelectionLength == 0;
            RegExPopCutMenu.Enabled = isSelected;
            RegExPopCopyMenu.Enabled = isSelected;
            RegExPopCopyCSMenu.Enabled = isSelected;
            RegExPopCopyVBMenu.Enabled = isSelected;
            RegExPopDeleteMenu.Enabled = isSelected;

            bool isAllSelected = txtBox.SelectionLength == txtBox.Text.Length;
            RegExPopSelectAllMenu.Enabled = isAllSelected;

            bool canPaste = Clipboard.GetDataObject() != null;
            RegExPopPasteMenu.Enabled = canPaste;
        }

        private void RegExPopCopyCSMenu_Click(object sender, System.EventArgs e)
        {
            ContextMenu contextMenu = ((System.Windows.Forms.MenuItem)sender).GetContextMenu();
            TextBox txtBox = (TextBox)contextMenu.SourceControl;

            Clipboard.SetDataObject(FormatStringForCS(txtBox.SelectedText), true);
        }

        private void RegExPopCopyVBMenu_Click(object sender, System.EventArgs e)
        {
            ContextMenu contextMenu = ((MenuItem)sender).GetContextMenu();
            TextBox txtBox = (TextBox)contextMenu.SourceControl;
            Clipboard.SetDataObject(FormatStringForVB(txtBox.SelectedText), true);
        }

        private void EditMenu_Popup(object sender, System.EventArgs e)
        {
            bool isRegExEntered = txtRegEx.Text.Length > 0;
            EditCopyRegexMenu.Enabled = isRegExEntered;
            EditCopyRegExCSMenu.Enabled = isRegExEntered;
            EditCopyRegExVBMenu.Enabled = isRegExEntered;
        }

        private void EditCopyRegexMenu_Click(object sender, System.EventArgs e)
        {
            Clipboard.SetDataObject(txtRegEx.Text, true);
        }

        private string FormatStringForCS(string unformattedString)
        {
            unformattedString = unformattedString.Replace("\\", "\\\\");
            return unformattedString.Replace("\"", "\\\"");
        }

        private string FormatStringForVB(string unformattedString)
        {
            return unformattedString.Replace("\"", "\"\"");
        }

        private void EditCopyRegExCSMenu_Click(object sender, System.EventArgs e)
        {
            Clipboard.SetDataObject("\"" + FormatStringForCS(txtRegEx.Text) + "\"", true);
        }

        private void EditCopyRegExVBMenu_Click(object sender, System.EventArgs e)
        {
            Clipboard.SetDataObject("\"" + FormatStringForVB(txtRegEx.Text) + "\"", true);
        }

        private void FileOpenTestMenu_Click(object sender, System.EventArgs e)
        {
            openFileDialog.Title = "Open Test File";
            DialogResult result = openFileDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                using (StreamReader testFileStream = new StreamReader(openFileDialog.FileName))
                {
                    rtbTestText.Text = testFileStream.ReadToEnd();
                }
            }
        }

        private void HelpFeedbackMenu_Click(object sender, System.EventArgs e)
        {
            Process.Start(@"http://regexeditor.codeplex.com/discussions");
        }

        private void FileSaveResultsAsMenu_Click(object sender, System.EventArgs e)
        {
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            DialogResult result = saveFileDialog.ShowDialog(this);

            if (result == DialogResult.OK) 
            {
                return; 
            }
            using (StreamWriter resultFileStream = new StreamWriter(saveFileDialog.FileName, false))
            {
                switch (saveFileDialog.FilterIndex)
                {
                    case 3:
                        if (this.dtMatchesRes == null) 
                        {
                            break; 
                        }
                        dtMatchesRes.WriteXml(resultFileStream);
                        break;
                    default:  /* txt, csv are default and are using same logic */
                        resultFileStream.Write(sbMatchesRes.ToString());
                        break;
                }
            }
        }

        /// <summary>
        /// Execute <see cref="System.Text.RegularExpressions.Regex" /> <see cref="System.Text.RegularExpressions.Match"/> 
        /// when keyboard key has been released. This action depends on Check state of cbMatchImmediately 
        /// <see cref="System.Windows.Forms.CheckBox" /> 
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
        private void txtRegEx_KeyUp(object sender, KeyEventArgs e)
        {
            // no immediate actions if not checked
            if (!cbMatchImmediately.Checked) 
            {
                return;
            }

            // Alt or / and Ctrl pressed
            if (e.KeyCode == Keys.Alt || e.KeyCode == Keys.Control) 
            {
                return;
            }

            switch ((int)e.KeyCode)
            {
                case 9:  // tab
                case 33:  // PageUp
                case 34:  // PageDn
                case 35:  // End
                case 36:  // Home
                case 37:  // arrow keys
                case 38:
                case 39:
                case 40:
                case 45:  // Insert
                case 46:  // Delete
                    return;
            }

            Debug.WriteLine("KeyCode: " + e.KeyCode + "\tKeyData: " + e.KeyData + "\tKeyValue: " + e.KeyValue);

            ApplyRegEx();
            e.Handled = true;
        }

        /// <summary>
        /// Applies the static <see cref="System.Text.RegularExpressions.Regex"/>.
        /// </summary>
        private void ApplyRegEx()
        {
            Regex re;
            bool err = false;
            ResetQueryText();
            ClearResultBox();  // clear previous shown results
            try
            {
                RegexOptions regExOptions = GetRegExOptions();
                re = new Regex(txtRegEx.Text, regExOptions);
                if (!ShowIsMatch(re))
                {
                    return;
                }
                FillData(re);
                ShowResult();
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);

                err = true;
                txtResult.Text = ex.Message;
                cbMatchResultsAsGrid.Checked = false;
            }
            finally 
            {
                re = null;
            }
            txtRegEx.BackColor = err ? Color.FromArgb(255, 182, 193) : Color.White;
        }

        /// <summary>
        /// Fills the data with <see cref="System.Text.RegularExpressions.MatchCollection"/>.
        /// </summary>
        /// <param name="re">The <see cref="System.Text.RegularExpressions.Regex"/> instance.</param>
        private void FillData(Regex re)
        {
            string[] groups = re.GetGroupNames();
            dtMatchesRes = new DataTable("MatchResult");
            sbMatchesRes = new StringBuilder();
            MatchCollection matches = re.Matches(rtbTestText.Text);
            int len = matches.Count;
            dtMatchesRes.Columns.AddRange(CreateGridCols());
            for (int i = 0; i < len; i++)
            {
                if (!matches[i].Success) 
                {
                    continue; 
                }
                ColorizeMatches(matches[i].Index, matches[i].Length);
                for (int j = 0; j < matches[i].Groups.Count; j++)
                {
                    if (String.IsNullOrEmpty(matches[i].Groups[j].Value)) 
                    {
                        continue; 
                    }
                    DataRow r = dtMatchesRes.NewRow();
                    r["#"] = j == 0 ? i.ToString() : String.Empty;
                    r["Index"] = j == 0 ? matches[i].Index.ToString() : String.Empty;
                    r["Length"] = j == 0 ? matches[i].Length.ToString() : String.Empty;
                    r["Group"] = groups[j];
                    r["Matched String"] = matches[i].Groups[j].Value;
                    dtMatchesRes.Rows.Add(r);
                    sbMatchesRes.AppendLine(j == 0
                                            ? String.Format("{0}, {1}, {2}, {3}, {4}", 
                                                            i, 
                                                            matches[i].Index,
                                                            matches[i].Length, 
                                                            groups[j], 
                                                            matches[i].Value)
                                            : String.Format(" , , , {0}, {1}", 
                                                            groups[j], 
                                                            matches[i].Groups[j].Value));
                }
            }
            grdResult.DataSource = dtMatchesRes;
            FormatResultGrid();
            ManageRowsVisibility();
            txtResult.Text = sbMatchesRes.ToString().Replace(", ", ",\t");
        }

        /// <summary>
        /// Manages the rows visibility and columns visibility depending on Show Groups checkbox state.
        /// <para>When the state has a true value all rows/columns will be visible. </para>
        /// <para>When value is different than true it is treated as null.</para>
        /// <para>Then visibility depends on whether there is a value in the 1st cell of the row</para>
        /// </summary>
        /// <param name="state">The state is <see cref="System.Nullable"/> <see cref="System.Boolean"/> value.</param>
        private void ManageRowsVisibility(bool? state = null)
        {
            if (grdResult.DataSource == null) 
            {
                return; 
            }
            CurrencyManager cm = (CurrencyManager)BindingContext[grdResult.DataSource]; // need to suspend buinding so as to change visibility state
            grdResult.Columns[3].Visible = state ?? false;
            int rowLen = cm.Count; // grdResult.RowCount;
            for (int i = 0; i < rowLen; i++)
            {
                cm.SuspendBinding();
                grdResult.Rows[i].Visible = state ?? !String.IsNullOrEmpty((string)grdResult.Rows[i].Cells[0].Value);
            }
        }

        /// <summary>
        /// Formats the result grid. Numbers (col 1 to 3) are right aligned, cols 4 and 5 are left aligned
        /// </summary>
        private void FormatResultGrid()
        {
            for (int i = 0; i < grdResult.Columns.Count; i++)
            {
                grdResult.Columns[i].DefaultCellStyle.Alignment = i < 3 ? DataGridViewContentAlignment.BottomRight : DataGridViewContentAlignment.BottomLeft;
            }
        }

        /// <summary>
        /// Shows the result of matches found depending on the state of the cbMatchResultsAsGrid checkbox.
        /// </summary>
        private void ShowResult()
        {
            if (cbMatchResultsAsGrid.Checked)
            {
                grdResult.Visible = true;
                txtResult.Visible = false;
            }
            else
            {
                grdResult.Visible = false;
                txtResult.Visible = true;
            }
        }

        /// <summary>
        /// Colorizes the matches in query text box.
        /// </summary>
        /// <param name="start">The start.</param>
        /// <param name="length">The length.</param>
        private void ColorizeMatches(int start, int length)
        {
            Color backColor;
            Color foreColor;
            rtbTestText.SuspendLayout();
            GetRandomColor(out backColor, out foreColor);
            rtbTestText.Select(start, length);
            rtbTestText.SelectionBackColor = backColor;
            rtbTestText.SelectionColor = foreColor;
            rtbTestText.ResumeLayout();
        }

        private void ResetQueryText()
        {
            rtbTestText.SuspendLayout();
            rtbTestText.SelectAll();
            rtbTestText.SelectionColor = Color.Black;
            rtbTestText.SelectionBackColor = Color.White;
            rtbTestText.ResumeLayout();
        }

        /// <summary>
        /// Shows the is match based on regex instance.
        /// </summary>
        /// <param name="re">The instance of <see cref="System.Text.RegularExpressions.Regex"/></param>
        /// <returns>
        /// A value indicating whether the IsMatch indicator.
        /// </returns>
        private bool ShowIsMatch(Regex re)
        {
            hasMatches = re.IsMatch(rtbTestText.Text);
            if (hasMatches)
            {
                lblIsMatchResult.Text = Convert.ToChar(82).ToString();
                lblIsMatchResult.BackColor = Color.Green;
            }
            else
            {
                ClearResultBox();
                lblIsMatchResult.Text = Convert.ToChar(81).ToString();
                lblIsMatchResult.BackColor = Color.Red;
                dtMatchesRes = null;
                sbMatchesRes = null;
            }
            return hasMatches;
        }

        /// <summary>
        /// Create columns for gridView based on maches properties
        /// </summary>
        /// <returns>
        /// A DataColumn formatted for the Match results.
        /// </returns>
        private DataColumn[] CreateGridCols()
        {
            DataColumn[] cols = new DataColumn[5];
            cols[0] = new DataColumn("#");
            cols[1] = new DataColumn("Index");
            cols[2] = new DataColumn("Length");
            cols[3] = new DataColumn("Group");
            cols[4] = new DataColumn("Matched String");
            sbMatchesRes.AppendLine("#, Index, Length, Group, Matched String");
            return cols;
        }        

        /// <summary>
        /// Gets the random color.
        /// </summary>
        /// <param name="back">
        /// </param>
        /// <param name="fore">
        /// </param>
        private void GetRandomColor(out Color back, out Color fore)
        {
            int r = random.Next(0, 255);
            int g = random.Next(0, 255);
            int b = random.Next(0, 255);
            back = Color.FromArgb(r, g, b);
            fore = Color.FromArgb((r > 128 ? r - 128 : r + 127), (g > 128 ? g - 128 : g + 127), (b > 128 ? b - 128 : b + 127));
        }

        private void FileMenu_Popup(object sender, EventArgs e)
        {
            FileSaveResultsAsMenu.Enabled = this.hasMatches;
        }

        #region [CheckBox actions]

        private void RegexOptions_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = sender as CheckBox;
            if (cbx == null)
            {
                return; 
            }
            switch (cbx.Name)
            {
                case "cbCompiled":
                case "cbCultureInvariant":
                case "cbExplicitCapture":
                case "cbIgnoreWhiteSpace":
                case "cbRightToLeft":
                case "cbSingleLine":
                    if (cbCompiled.Checked || cbCultureInvariant.Checked || cbExplicitCapture.Checked ||
                             cbIgnoreWhiteSpace.Checked || cbRightToLeft.Checked || cbSingleLine.Checked)
                    {
                        cbECMAScript.Enabled = false;
                    }
                    else
                    {
                        cbECMAScript.Enabled = true; 
                    }
                    break;

                case "cbECMAScript":
                    cbCompiled.Enabled = !cbx.Checked;
                    cbCultureInvariant.Enabled = !cbx.Checked;
                    cbExplicitCapture.Enabled = !cbx.Checked;
                    cbIgnoreWhiteSpace.Enabled = !cbx.Checked;
                    cbRightToLeft.Enabled = !cbx.Checked;
                    cbSingleLine.Enabled = !cbx.Checked;
                    break;
            }
            ApplyRegEx();
        }

        private void cbMatchResultsAsGrid_CheckedChanged(object sender, EventArgs e)
        {
            cbGroupsVisibility.Enabled = (sender as CheckBox).Checked;
            ShowResult();
        }

        private void cbGroupsVisibility_CheckedChanged(object sender, EventArgs e)
        {
            if ((sender as CheckBox).Checked) 
            {
                ManageRowsVisibility(true); 
            }
            else
            {
                ManageRowsVisibility(); 
            }
        }

        #endregion

        private void rtbTestText_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Z:
                    if (e.Control)
                    {
                        rtbTestText.SuspendLayout();
                        rtbTestText.Text = Clipboard.GetText(TextDataFormat.Rtf);
                        rtbTestText.ResumeLayout();
                    }
                    break;
            }
        }
    }
}
